********************************* Stata Do File ********************************
*********************** The Disagreement We Overlook: **************************
****** Examining Core and Acquaintance Networks in a Presidential Primary ******
********************************** Sept 2024 ***********************************


******************************** 1. Read in Data *********************************

*import delimited "/R&PSubmission.csv"

***************************** 2. Results in Text *******************************

****** Section 2 = Number of Democrats in sample ****** 
tab pid7_w1
tab pid7_w1 if pid7_w1<=3


****** Section 3 ****** 
tab disc1_w1 if pid7_w1<=3
tab disc2_w1 if pid7_w1<=3
tab disc3_w1 if pid7_w1<=3
tab disc4_w1 if pid7_w1<=3

tab totaldisc_w1
tab totaldisc_w1 if pid7_w1<=3
sum totaldisc_w1 if pid7_w1<=3, detail


****** Section 3.1 = Dem's Core Networks Contain Dems ****** 
tab demdisc1_w1 if pid7_w1<=3
tab demdisc2_w1 if pid7_w1<=3
tab demdisc3_w1 if pid7_w1<=3
tab demdisc4_w1 if pid7_w1<=3

tab totaldemdisc_w1
tab totaldemdisc_w1 if pid7_w1<=3
sum totaldemdisc_w1 if pid7_w1<=3, detail
	
****** Section 3.2 = Dems disagree regularly w/ each other ****** 
tab demdis01disc1_w1 if pid7_w1<=3	
tab demdis01disc2_w1 if pid7_w1<=3
tab demdis01disc3_w1 if pid7_w1<=3 		
tab demdis01disc4_w1 if pid7_w1<=3

tab disagreement_mean if pid7_w1<=3

**** Fig 1: conducted in R ****
	* See Main.R

*library(ggridges)
*library(dplyr)

*setwd("~/Library/CloudStorage/Dropbox")
*my_data <- read.csv("")

*library(ggplot2)

*plot_data2 <- my_data %>% filter(!is.na(totaldisc_w1))

*# ALL DISCUSSANTS: Disagreement MEAN w/out title & add mean
*ggplot(filter(plot_data2, pid7_w1 <4), aes(x = disagreement_mean, y = factor(totaldisc_w1))) +
 * geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) + 
  *xlab("Frequency of Disagreement") + ylab("Total Number of Discussants") +
  *scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
*  theme_classic()

*# DEM DISCUSSANTS DISAGREEMENT w/out title & add mean
*ggplot(filter(plot_data2, pid7_w1 <4), aes(x = disagreement_mean, y = factor(totaldemdisc_w1))) +
  *stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) + 
  *xlab("Frequency of Disagreement") + ylab("Number of Democratic Discussants") +
  *scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
  *theme_classic()


****** Section 3.3 = Opinions Vary between Dems ****** 
tab simdisc1_w1 if pid7_w1<=3
tab simdisc2_w1 if pid7_w1<=3
tab simdisc3_w1 if pid7_w1<=3
tab simdisc4_w1 if pid7_w1<=3

tab similarity_mean if pid7_w1<=3

**** Fig 2: conducted in R ****
	* See Main.R

*# ALL DISCUSSANTS:  MEAN Similarity w/out title
*ggplot(filter(plot_data2, pid7_w1 <4), aes(x = similarity_mean, y = factor(totaldisc_w1))) +
 *geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) + 
  *xlab("Perceived Opinion Similarity") + ylab("Total Number of Discussants") +
  *scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
 * theme_classic()

*# DEM DISCUSSANTS:  MEAN Similarity w/out title
*ggplot(filter(plot_data2, pid7_w1 <4), aes(x = similarity_mean, y = factor(totaldemdisc_w1))) +
 * geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) + 
  *xlab("Perceived Opinion Similarity") + ylab("Number of Democratic Discussants") +
  *scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
  *theme_classic()	
  
  
****** Section 3.4 = Core Networks and Primary Candidate Support ****** 

**** Ttest: Number of Democratic Discussants by Candidate Choice *****
ttest totaldemdisc_w1, by(biden)
ttest totaldemdisc_w1, by(sanders)
ttest totaldemdisc_w1, by(bloomberg)
ttest totaldemdisc_w1, by(warren)

**** Fig 3: conducted in R ****
	* See Main.R

*plot_data1 <- my_data1 %>% filter(!is.na(big_four))

*# Disagreement - No Title w/ Mean, 1-5 scale # 
*ggplot(filter(plot_data1, pid7_w1 <4), aes(x = disagreement_mean, y = factor(big_four))) +
 * stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) +
  *xlab("Frequency of Disagreement") + ylab("Primary Candidate Supported") + 
  *scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
  *theme_classic()

*# Similarity - No Title w/ Mean # 
*ggplot(filter(plot_data1, pid7_w1 <4), aes(x = similarity_mean, y = factor(big_four))) +
*  stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) +
*  xlab("Percieved Opinion Similarity") + ylab("Primary Candidate Supported") +
*  scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
*  theme_classic()


****** Section 4.1 = Acquaintance Networks, ARD ****** 

**** Fig 4 ****
	
*** Guns ***
collapse (mean) meangun01 = gun01 (sd) sdgun01=gun01 (count) n=gun01, by(big_four)

gen higun01 = meangun01 + invttail(n-1, 0.025)*(sdgun01 / sqrt(n))
gen lowgun01 = meangun01 - invttail(n-1, 0.025)*(sdgun01 / sqrt(n))

twoway (bar meangun01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meangun01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meangun01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meangun01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap higun01 lowgun01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Own a Gun) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	 	
	
*** Trump ***
collapse (mean) meantrump01 = trump01 (sd) sdtrump01=trump01 (count) n=trump01, by(big_four)

gen hitrump01 = meantrump01 + invttail(n-1, 0.025)*(sdtrump01 / sqrt(n))
gen lowtrump01 = meantrump01 - invttail(n-1, 0.025)*(sdtrump01 / sqrt(n))

twoway (bar meantrump01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meantrump01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meantrump01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meantrump01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hitrump01 lowtrump01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Trump Voters) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	

*** Clinton ***
collapse (mean) meanclinton01 = clinton01 (sd) sdclinton01=clinton01 (count) n=clinton01, by(big_four)

gen hiclinton01 = meanclinton01 + invttail(n-1, 0.025)*(sdclinton01 / sqrt(n))
gen lowclinton01 = meanclinton01 - invttail(n-1, 0.025)*(sdclinton01 / sqrt(n))

twoway (bar meanclinton01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanclinton01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanclinton01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanclinton01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hiclinton01 lowclinton01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Clinton Voters) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** LgbtQ ***
collapse (mean) meanlgbtQ01 = lgbtQ01 (sd) sdlgbtQ01=lgbtQ01 (count) n=lgbtQ01, by(big_four)

gen hilgbtQ01 = meanlgbtQ01 + invttail(n-1, 0.025)*(sdlgbtQ01 / sqrt(n))
gen lowlgbtQ01 = meanlgbtQ01 - invttail(n-1, 0.025)*(sdlgbtQ01 / sqrt(n))

twoway (bar meanlgbtQ01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanlgbtQ01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanlgbtQ01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanlgbtQ01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hilgbtQ01 lowlgbtQ01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(LGBT) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Police ***
collapse (mean) meanpolice01 = police01 (sd) sdpolice01=police01 (count) n=police01, by(big_four)

gen hipolice01 = meanpolice01 + invttail(n-1, 0.025)*(sdpolice01 / sqrt(n))
gen lowpolice01 = meanpolice01 - invttail(n-1, 0.025)*(sdpolice01 / sqrt(n))

twoway (bar meanpolice01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanpolice01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanpolice01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanpolice01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hipolice01 lowpolice01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Police) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Military ***
collapse (mean) meanmilitary01 = military01 (sd) sdmilitary01=military01 (count) n=military01, by(big_four)

gen himilitary01 = meanmilitary01 + invttail(n-1, 0.025)*(sdmilitary01 / sqrt(n))
gen lowmilitary01 = meanmilitary01 - invttail(n-1, 0.025)*(sdmilitary01 / sqrt(n))

twoway (bar meanmilitary01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanmilitary01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanmilitary01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanmilitary01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap himilitary01 lowmilitary01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Military) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Africanamer ***
collapse (mean) meanafricanamer01 = africanamer01 (sd) sdafricanamer01=africanamer01 (count) n=africanamer01, by(big_four)

gen hiafricanamer01 = meanafricanamer01 + invttail(n-1, 0.025)*(sdafricanamer01 / sqrt(n))
gen lowafricanamer01 = meanafricanamer01 - invttail(n-1, 0.025)*(sdafricanamer01 / sqrt(n))

twoway (bar meanafricanamer01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanafricanamer01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanafricanamer01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanafricanamer01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hiafricanamer01 lowafricanamer01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(African American) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Attendchurch ***
collapse (mean) meanattendchurch01 = attendchurch01 (sd) sdattendchurch01=attendchurch01 (count) n=attendchurch01, by(big_four)

gen hiattendchurch01 = meanattendchurch01 + invttail(n-1, 0.025)*(sdattendchurch01 / sqrt(n))
gen lowattendchurch01 = meanattendchurch01 - invttail(n-1, 0.025)*(sdattendchurch01 / sqrt(n))

twoway (bar meanattendchurch01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanattendchurch01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanattendchurch01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanattendchurch01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hiattendchurch01 lowattendchurch01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Attend Church) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Muslim ***
collapse (mean) meanmuslim01 = muslims01 (sd) sdmuslim01=muslims01 (count) n=muslims01, by(big_four)

gen himuslim01 = meanmuslim01 + invttail(n-1, 0.025)*(sdmuslim01 / sqrt(n))
gen lowmuslim01 = meanmuslim01 - invttail(n-1, 0.025)*(sdmuslim01 / sqrt(n))

twoway (bar meanmuslim01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanmuslim01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanmuslim01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanmuslim01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap himuslim01 lowmuslim01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Muslims) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 
	   
*** Bornoutsideus ***
collapse (mean) meanbornoutsideus01 = bornoutsideus01 (sd) sdbornoutsideus01=bornoutsideus01 (count) n=bornoutsideus01, by(big_four)

gen hibornoutsideus01 = meanbornoutsideus01 + invttail(n-1, 0.025)*(sdbornoutsideus01 / sqrt(n))
gen lowbornoutsideus01 = meanbornoutsideus01 - invttail(n-1, 0.025)*(sdbornoutsideus01 / sqrt(n))

twoway (bar meanbornoutsideus01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanbornoutsideus01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanbornoutsideus01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanbornoutsideus01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hibornoutsideus01 lowbornoutsideus01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Immigrants) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 
	   
*** Unemployed ***
collapse (mean) meanunemployed01 = unemployed01 (sd) sdunemployed01=unemployed01 (count) n=unemployed01, by(big_four)

gen hiunemployed01 = meanunemployed01 + invttail(n-1, 0.025)*(sdunemployed01 / sqrt(n))
gen lowunemployed01 = meanunemployed01 - invttail(n-1, 0.025)*(sdunemployed01 / sqrt(n))

twoway (bar meanunemployed01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meanunemployed01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meanunemployed01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meanunemployed01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap hiunemployed01 lowunemployed01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Unemployed) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
*** Graddegree ***
collapse (mean) meangraddegree01 = graddegree01 (sd) sdgraddegree01=graddegree01 (count) n=graddegree01, by(big_four)

gen higraddegree01 = meangraddegree01 + invttail(n-1, 0.025)*(sdgraddegree01 / sqrt(n))
gen lowgraddegree01 = meangraddegree01 - invttail(n-1, 0.025)*(sdgraddegree01 / sqrt(n))

twoway (bar meangraddegree01 big_four if big_four==1, barwidth(0.5) bcolor(edkblue)) ///
	   (bar meangraddegree01 big_four if big_four==2, barwidth(0.5) bcolor(ebblue)) ///
	   (bar meangraddegree01 big_four if big_four==3, barwidth(0.5) bcolor(eltblue)) ///
	   (bar meangraddegree01 big_four if big_four==4, barwidth(0.5) bcolor(ebg)) ///	   
	   (rcap higraddegree01 lowgraddegree01 big_four) ///
	   , xlabel(1 "Sanders" 2 "Warren" 3 "Biden" 4 "Bloomberg") ///
       ylabel(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1") ///
	   subtitle(Advanced Degree) ///
	   graphregion(fcolor(white)) ///
       legend(off) ytitle("Proportion of Respondents Who Know 2 People or More") xtitle("") 	
	   
**** Combine ****	   

**** ttests in text ****	   
ttest graddegree01, by(warren)
ttest africanamer01, by(biden)
ttest lgbtQ01, by(biden)


****** Section 4.2 = Dendrograms (done in R), ARD ****** 
